/*******************#XipFileLock#******************
 *函 数 名: XipFileLock
 *函数功能: 文件锁
 *函数作者: zhangl
 *版 本 号: 1.0.0
 *创建日期: 20160318
输入: type 1加锁 2解锁
      fullname文件全路径
******************************************************/
#include "kernel/syspub.h"
#include "kernel/xipcmstc.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
int XipFileLock(int type,char *fullname)
{
    int fd;
    struct  flock lock;
    int ret=1;


    fd=open(fullname,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
    if(fd<=0)
    {
        APPLOG("D","打开文件锁文件错误[%s]",fullname);
        return -5;
    }

    if(type==1)
    {
        lock.l_type=F_WRLCK;
    }
    else if(type==2)
    {
        lock.l_type=F_UNLCK;
    }
    else
    {
        APPLOG("D","标志错误");
        return -10;
    }
    lock.l_start=0;
    lock.l_whence=SEEK_SET;
    lock.l_len=0;

    if(fcntl(fd,F_SETLK,&lock))
    {
        if(type==1)
        {
            APPLOG("D","文件锁存在");
        }
        else
        {
            APPLOG("D","文件锁不存在");
        }
        
        return 1;
    }
    else
    {
        if(type==1)
        {
            APPLOG("D","创建文件锁成功[%s]",fullname);
        }
        else
        {
            APPLOG("D","解除文件锁成功[%s]",fullname);
        }
    }

    /*
    close(fd);
    */
    return 0;
}